package catchData.core;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;

public class DoPostBack1 implements Runnable{

	private Logger log = Logger.getLogger("DoPostBack1");
	private CatchDataUtil catchData = new CatchDataUtil();
	
	private String webURL;
	private String folderPath;
	private int startPage;
	private int endPage;
	private String __EVENTTARGET;
	private String __EVENTARGUMENT;
	private String encode;
	
	public DoPostBack1(String webURL, String folderPath, int startPage, int endPage,
				String __EVENTTARGET, String __EVENTARGUMENT, String encode) {
		this.webURL = webURL;
		this.folderPath = folderPath;
		this.startPage = startPage;
		this.endPage = endPage;
		this.__EVENTTARGET = __EVENTTARGET;
		this.__EVENTARGUMENT = __EVENTARGUMENT;
		this.encode = encode;
	}

	public void run() {
		String str = "";
		PostMethod post = null;
		HttpClient client = catchData.initHttpClient();
		try {
			for (int i = startPage; i <= endPage; i++) {
				post = new PostMethod(webURL);
				catchData.setHeaders(post,webURL,catchData.getWebSite(webURL));
//				NameValuePair eventTarget = new NameValuePair("__EVENTTARGET", __EVENTTARGET);
//				NameValuePair eventArgument = new NameValuePair("__EVENTARGUMENT",__EVENTARGUMENT+i);
//				post.setRequestBody(new NameValuePair[] { eventTarget,eventArgument });
				NameValuePair tableName = new NameValuePair("TableName", "S20130827164900");
				NameValuePair page = new NameValuePair("Page", "" + i);
				post.setRequestBody(new NameValuePair[] { tableName,page });
				client.executeMethod(post);
				str = post.getResponseBodyAsString();
//				System.out.println("the content for the page "+ i + " is :" +str);
				catchData.saveDataFile(folderPath, str, i, encode);
				post.releaseConnection();
			}
		} catch (Exception e) {
			log.info(e);
		}
	}

	public String getWebURL() {
		return webURL;
	}

	public void setWebURL(String webURL) {
		this.webURL = webURL;
	}

	public String getFolderPath() {
		return folderPath;
	}

	public void setFolderPath(String folderPath) {
		this.folderPath = folderPath;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public String get__EVENTTARGET() {
		return __EVENTTARGET;
	}

	public void set__EVENTTARGET(String __EVENTTARGET) {
		this.__EVENTTARGET = __EVENTTARGET;
	}

	public String get__EVENTARGUMENT() {
		return __EVENTARGUMENT;
	}

	public void set__EVENTARGUMENT(String __EVENTARGUMENT) {
		this.__EVENTARGUMENT = __EVENTARGUMENT;
	}

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}
}